//
//  EightView.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/2.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class EightView: UIView {

    fileprivate var allBtArray = NSMutableArray();
    fileprivate var _selectBt:EightButton?;
    
    override init(frame: CGRect) {
        super.init(frame:frame);
        
        self.backgroundColor = .lightGray;
        
        let itemWidth = self.frame.width/3.0;
        
        let bt1 = self.onCreateBtForView(name: "首页", img: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), selectImg: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(),_tag: 100);
        bt1.frame = CGRect(x: 0, y: 0, width: itemWidth, height: self.frame.height);
        bt1.isSelected = true;
        _selectBt = bt1;
        
        let bt2 = self.onCreateBtForView(name: "", img: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), selectImg: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(),_tag: 101);
        bt2.frame = CGRect(x: itemWidth, y: 0, width: itemWidth, height: self.frame.height);
        
        let bt3 = self.onCreateBtForView(name: "我的", img: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), selectImg: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(),_tag: 102);
        bt3.frame = CGRect(x: itemWidth*2, y: 0, width: itemWidth, height: self.frame.height);
        
        allBtArray.add(bt1);
        allBtArray.add(bt2);
        allBtArray.add(bt3);
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func onCreateBtForView(name:String,img:UIImage,selectImg:UIImage,_tag:NSInteger) -> EightButton {
        let bt1 = EightButton(type: .custom);
        bt1.setTitle(name, for: .normal)
        bt1.setTitleColor(.white, for: .normal);
        bt1.setTitleColor(.red, for: .selected);
        bt1.titleLabel?.textAlignment = .center;
        bt1.setImage(img, for: .normal)
        bt1.setImage(selectImg, for: .selected);
        bt1.imageView?.contentMode = .scaleAspectFit;
        bt1.backgroundColor = .clear;
        bt1.tag = _tag;
        bt1.addTarget(self, action: #selector(onClickBtForView(bt:)), for: .touchUpInside);
        self.addSubview(bt1);
        return bt1;
    }
    
    //TODO:Actions
    @objc fileprivate func onClickBtForView(bt:EightButton) {
        if bt == _selectBt {
            return;
        }
        
        allBtArray.enumerateObjects { (obj, index, stop) in
            let bt:EightButton = obj as! EightButton;
            bt.isSelected = false;
        }
        bt.isSelected = true;
        _selectBt = bt;
    }
}

class EightButton:UIButton {
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        return CGRect(x: 0, y: contentRect.size.height-20, width: contentRect.size.width, height: 20);
    }
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        if self.titleLabel?.isHidden == true {
            return CGRect(x: 0, y: 10, width: contentRect.size.width, height: contentRect.size.height-20);
        }
        return CGRect(x: 0, y: 10, width: contentRect.size.width, height: contentRect.size.height-40);
    }
}
